<template>
	<div class="login_container">
		<!-- 登录盒子-->
		<div class="login_box">
			<!-- 头像盒子-->
			<div class="img_box">
				<!--  <img src="./assets/logo.png" /> -->
			</div>
			<!-- 表单-->
			<div class="form_box">
				<el-form ref="form" :model="form" label-width="80px">
					<el-form-item label="账号">
						<el-input v-model="form.account"></el-input>
					</el-form-item>
					<el-form-item label="密码">
						<el-input type="password" v-model="form.password"></el-input>
					</el-form-item>

					<el-form-item>
						<el-button type="primary" @click="login()">登录</el-button>
						<el-button @click="reg()">注册</el-button>
					</el-form-item>
				</el-form>
			</div>
		</div>
	</div>
</template>

<script>
	/* 导出组件 其他地方才可以导入组件*/
	export default {
		data() {
			return {
				form: {
					account: "admin",
					password: "123"
				},
				flag: true
			}
		},
		methods: {
			login() {
				var that = this;
				//向后端发送ajax请求,提交表单的数据   json格式提交 name=11&pwd=11
				this.$http.post('/user/login', this.form).then(function(res) { //回调
					console.log(res)
					if (res.data.code == 200) {
						//在前端保存用户信息
						window.sessionStorage.setItem("account", res.data.user.account); //在浏览器中保存
						window.sessionStorage.setItem("userId", res.data.user.id); //在浏览器中保存
						window.sessionStorage.setItem("type", res.data.user.type); //在浏览器中保存
						window.sessionStorage.setItem("name", res.data.user.nickname); //在浏览器中保存
						window.sessionStorage.setItem("token", res.data.token); //在浏览器中保存
						window.sessionStorage.setItem("active", res.data.userStatus);
						//跳转到登录成功页面
						if (res.data.user.type == 2) { //游客登录
							that.$router.push("/homepage");
						} else {
							that.$router.push("/succ");
						}
						that.$message({
							message: res.data.msg,
							type: 'success'
						});
					} else if (res.data.code == 201) {
						that.$message({
							message: res.data.msg,
							type: 'warning'
						});
					} else {
						console.log(res.data.msg)
						that.$message.error({
							message: res.data.msg
						});
					}
				});
			},
			reg() {
				this.$router.push("/reg");
			}
		}
	}
</script>

<style scoped="scoped">
	html,
	body,
	.login_container {
		height: 100%;
		margin: 0px;
		padding: 0px;
	}

	.login_container {
		background-color: #00a7fa;
	}

	.login_box {
		width: 450px;
		height: 350px;
		background-color: #fff;
		border-radius: 10px;
		position: absolute;
		left: 50%;
		top: 50%;
		margin-top: -175px;
		margin-left: -225px;
	}

	.img_box {
		width: 130px;
		height: 130px;
		position: absolute;
		left: 50%;
		transform: translate(-50%, -50%);
		background-color: #fff;
		border-radius: 50%;
		padding: 5px;
		border: 1px solid #eee;
	}

	.img_box img {
		width: 100%;
		height: 100%;
		border-radius: 50%;
		background-color: #eee;
	}

	.form_box {
		margin-top: 100px;
		padding: 10px;
	}
</style>
